package com.starnet.studentmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.starnet.studentmanagement.entity.CourseSelection;
import com.starnet.studentmanagement.entity.Courses;
import com.starnet.studentmanagement.entity.Students;
import com.starnet.studentmanagement.handler.ApiException;
import com.starnet.studentmanagement.mapper.CourseSelectionMapper;
import com.starnet.studentmanagement.mapper.CoursesMapper;
import com.starnet.studentmanagement.mapper.StudentsMapper;
import com.starnet.studentmanagement.service.CourseSelectionService;
import com.starnet.studentmanagement.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sangeng
 * @since 2022-03-26
 */
@Service
public class CourseSelectionServiceImpl implements CourseSelectionService {
    @Autowired
    CourseSelectionMapper courseSelectionMapper;
    @Autowired
    CoursesMapper coursesMapper;
    @Autowired
    StudentsMapper studentsMapper;
    @Transactional
    @Override
    public Result addNewGrade(CourseSelection courseSelection) {
        //首先检查有没有此学生
        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper.eq(Students::getSid, courseSelection.getSid());
        Students students = studentsMapper.selectOne(studentsLambdaQueryWrapper);
        if (students == null) {
            throw new ApiException(ResultCode.BAD_PARAM.getCode(), "此学号学生未在学生管理系统中注册");
        }
        LambdaQueryWrapper<Courses> coursesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        coursesLambdaQueryWrapper.eq(Courses::getCid, courseSelection.getCid());
        Courses courses = coursesMapper.selectOne(coursesLambdaQueryWrapper);
        if (courses == null) {
            throw new ApiException(ResultCode.BAD_PARAM.getCode(), "此课程号未在课程管理系统中注册");
        }
        int row = courseSelectionMapper.insert(courseSelection);
        if (row == 0) {
            throw new ApiException(ResultCode.UNKNOWN_ERROR.getCode(), "加入新课程失败，服务端未知错误");
        }
        return Result.success(null);
    }

    @Override
    public Result updateGrade(CourseSelection courseSelection) {
        int i = courseSelectionMapper.updateById(courseSelection);
        if (i == 0) {
            throw new ApiException(ResultCode.NOT_FOUND.getCode(), "更新失败,数据已被删除");
        }
        return Result.success(null);
    }

    @Override
    public Result deleteGradeById(Integer id) {
        int i = courseSelectionMapper.deleteById(id);
        if (i == 0) {
            throw new ApiException(ResultCode.NOT_FOUND.getCode(), "删除失败，数据不存在或已删除");
        }
        return Result.success(null);
    }

    @Override
    public PageResult listAll(Integer page, Integer pageSize, String cid) {
        if (pageSize == null) {
            pageSize = 10;
        }
        int limit = pageSize;
        int offset = (page - 1) * pageSize;
        List<GradeVO> gradeVos = courseSelectionMapper.listAllOrByCid(cid, limit, offset);
        if (gradeVos.size() == 0) {
            throw new ApiException(ResultCode.NOT_FOUND.getCode(), "无匹配记录");
        }
        int total = courseSelectionMapper.countAllOrByCid(cid);
        int totalPages = (int) Math.ceil((double) total / (double) pageSize);
        PageVO pageVO = new PageVO(page, pageSize, total, totalPages);

        return PageResult.success(gradeVos, pageVO);
    }
    @Transactional
    @Override
    public Result getPassRate(String cid) {
        //验证是否有此课程号的课程
        LambdaQueryWrapper<Courses> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Courses::getCid, cid);
        Courses course = coursesMapper.selectOne(queryWrapper);
        if (course == null) {
            throw new ApiException(ResultCode.NOT_FOUND.getCode(), "此课程号不存在");
        }
        //获取参与该课程的全部学生人数
        int totalNumber = courseSelectionMapper.countAllOrByCid(cid);
        //获取过了多少人
        int passNumber = courseSelectionMapper.getPassNumber(cid);
        double passRate = (double) passNumber / (double) totalNumber;
        //设置浮点数格式 保留两位小数  防止33.333333333333%的情况
        DecimalFormat format = new DecimalFormat("#.00");
        String passNumberInFormat = format.format(passRate*100);
        PassVO passVO = new PassVO();
        BeanUtils.copyProperties(course, passVO);
        passVO.setPassRate(passNumberInFormat+"%");
        passVO.setPassNumber(passNumber);
        passVO.setTotalNumber(totalNumber);
        return Result.success(passVO);
    }


}
